package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/tool"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type TraceabilityAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTraceabilityAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TraceabilityAddLogic {
	return &TraceabilityAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TraceabilityAddLogic) TraceabilityAdd(in *cardclient.TraceabilityAddRequest) (*cardclient.TraceabilityAddResponse, error) {
	data := entity.CardTraceabilityReq{
		CardNumber:     in.CardNumber,
		StorageAdminId: in.StorageAdminId,
		StorageTime:    tool.StringToTime(in.StorageTime),
		PublishAdminId: in.PublishAdminId,
		PublishTime:    tool.StringToTime(in.PublishTime),
		ReceiveAdminId: in.ReceiveAdminId,
		ReceiveTime:    tool.StringToTime(in.ReceiveTime),
		BindingUserId:  in.BindingUserId,
		BindingTime:    tool.StringToTime(in.BindingTime),
		WriteOffTime:   tool.StringToTime(in.WriteOffTime),
		Status:         in.Status,
	}

	var ctx context.Context
	_, err := l.svcCtx.TraceabilityModel.Insert(ctx, &data)

	if err != nil {
		return nil, err
	}

	return &cardclient.TraceabilityAddResponse{}, nil
}
